# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import json
from kafka import KafkaProducer

from repit_jc.items import GameListItem


class RepitJcPipeline(object):

    def __init__(self):
        print("进入Kafka初始化方法")
        self.producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'),
                                      bootstrap_servers=['192.168.215.10:9092', '192.168.215.20:9092',
                                                         '192.168.215.30:9092'])
        print(self, "--------------------------")

    def process_item(self, item, spider):
        if isinstance(item, GameListItem):
            self.process_item_to_kafka(item)
        return item

    def process_item_to_kafka(self, item):
        print("==============================")
        tmp = {
            'game_name': item["game_name"],
            'game_author': item["game_author"],
            'game_type': item["game_type"],
            'game_description': item["game_description"],
            'game_rank': item["game_rank"],
            'game_score': item["game_score"],
        }

        self.producer.send('game_list', tmp)
        print(self)
        pass
